$(function() {
	var phoneReg = /(^((0[1,2]{1}\d{1}-?\d{8})|(0[3-9]{1}\d{2}-?\d{7,8}))$)|(^0?(13[0-9]|15[0-35-9]|18[01236789]|14[57])[0-9]{8}$)/;
	var userAdd = "<div class='addContent' style='display: none;'>"
			+ "             <form id='userAddForm' action='${pageContext.request.contextPath }/clientuser/addUser.action' method='post' enctype='multipart/form-data'>"
			+ "               <table>"
			+ "                  <tr>"
			+ "                    <td>手机号码:</td>"
			+ "			          <td><input name='clientUserDto.userName' class='text {required:true,phone:true,remote:\"#path/clientuser/checkUserName.action\",messages:{required:\"请输入用户名！\",remote:\"手机号已存在请使用其他手机号!\"}}' type='text' /></td>"
			+ "                  </tr>"
			+ "           		<tr>"
			+ "			          <td>密码:</td>"
			+ "         			  <td><input id='password' name='clientUserDto.password' class='text {required:true,minlength:4,maxlength:10,messages:{required:\"请输入密码！\",minlength:\"请输入长度在4-10之间的密码\",maxlength:\"请输入长度在4-10之间的密码\"}}' type='password' /></td>"
			+ "           		</tr>"
			+ "           		<tr>"
			+ "         			  <td>再次确认密码:</td>"
			+ "          		  <td><input class='text {required:true,equalTo:\"#password\",messages:{required:\"请再次输入密码！\",equalTo:\"两次输入的密码必须相同！\"}}' type='password' /></td>"
			+ "		           </tr>"
			+ "	      	       <tr>"
			+ "			        <td>昵称:</td>"
			+ "			        <td><input 	class='text {minlength:2,maxlength:10,messages:{minlength:\"请输入长度在2-6之间的昵称\",maxlength:\"请输入长度在2-10之间的昵称\"}}'	name='clientUserDto.userAilas' type='text' size='50' /></td>"
			+ "		           </tr>"
			+ "		           <tr>"
			+ "			        <td>头像:</td>"
			+ "			        <td><input class='text' name='fileMaterial' type='file' /></td>"
			+ "          	   </tr>"
			+ "	   	           <tr>"
			+ "			        <td colspan='2' align='right'><input type='button' value='确定'	id='ok' /><input type='button' value='取消' id='cancel' /></td>"
			+ "	 	           </tr>" + "     	      </table>"
			+ "           </form>" + "     </div>";
	function stripHtml(value) {
		return value.replace(/<.[^<>]*?>/g, ' ')
				.replace(/&nbsp;|&#160;/gi, ' ').replace(
						/[0-9.(),;:!?%#$'"_+=\/-]*/g, '');
	}
	$.validator.addMethod("phone", function(value, element) {
		return this.optional(element) || phoneReg.test(value);
	}, "请输入正确的手机号码!");
	var $deviceModel = $("select[name='condition.deviceModel']");
	$("#allPicker").click(function() {
		var flag = true;
		if ($("#allPicker").attr("checked") == true) {
			flag = true;
		} else {
			flag = false;
		}
		$("input[name='ckUserBox']").each(function() {
			$(this).attr("checked", flag);
		});
	});
	$("#delAll").click(function() {
		$("input[name='ckUserBox']").each(function() {
			$(this).attr("checked", false);
		});
	});
	$("#antiAll").click(function() {
		$("input[name='ckUserBox']").each(function() {
			$(this).attr("checked", !this.checked);
		});
	});
	$("#restBtn").click(function() {
		$("#userType")[0].selectedIndex = 0;
		$("#deviceTypeName").val("");
		$("#update_activeEndTime").val("");
		$("#update_activeStartTime").val("");
		$("#userName").val("");
	});
	$deviceModel.dropdownchecklist({
		firstItemChecksAll : true,
		width : 200,
		maxDropHeight : 300
	});
	$("a#userAdd").click(function() {
		var $userAddDiv = $(userAdd);
		var addSuccess = function(msg) {
			var url = window.location.href;
			if (url.indexOf("?") == -1) {
				url = url + "?condition.userType=NOT_VERIF";
			} else {
				url = url + "&condition.userType=NOT_VERIF";
			}
			if (msg == "error") {
				alert("添加用户失败,请稍后再试");
				return;
			} else if (msg == "success") {
				window.location.href = url;
			}
		};
		var addError = function() {
			alert("添加用户失败,请稍后再试");
		}
		$.blockUI({
			theme : true,
			title : "添加用户",
			message : $userAddDiv,
			themedCSS : {
				width : '395px'
			},
		});
		$userAddDiv.find("#cancel").click(function() {
			var $input = $userAddDiv.find("form input.text");
			$input.val("");
			$.unblockUI();
		});
		$userAddDiv.find("#ok").click(function() {
			var $form = $userAddDiv.find("form");
			if (!$form.valid()) {
				return;
			}
			var options = {
				url : path + '/clientuser/addUsers.action',
				type : "POST",
				dataType : "text",
				async : false,
				success : addSuccess,
				error : addError
			}
			$form.ajaxSubmit(options);
		});
		$userAddDiv.find("form").validate();
	});
});